package cn.com.surker.production.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.production.invoice.InvoiceProcessEnum;
import cn.com.surker.production.entity.ProductionIndentDispatch;
import cn.com.surker.production.entity.vo.DispatchVo;
import cn.com.surker.production.service.ErpIndentDispatchService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * @author luotao
 * @date 2024/9/26
 */
@Service
public class ErpIndentDispatchImpl extends IndentDispatchServiceBaseImpl implements ErpIndentDispatchService {

	@Override
	public List<ProductionIndentDispatch> findList(String code, Long indentId, Long lineId, Integer... proStatus) {
		QueryWrapper<ProductionIndentDispatch> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		if (StringUtils.isNotBlank(code)) {
			qw.eq("code", code);
		}
		if (indentId != null) {
			qw.eq("indent_id", indentId);
		}
		if (lineId != null) {
			qw.eq("line_id", lineId);
		}
		if (proStatus != null && proStatus.length > 0) {
			qw.in("pro_status", proStatus);
		}
		List<ProductionIndentDispatch> list = indentDispatchDao.selectList(qw);
		return list;
	}

	@Override
	public List<DispatchVo> findFullList(String code, Long indentId, Long lineId, Integer... proStatus) {
		QueryWrapper<ProductionIndentDispatch> qw = new QueryWrapper<>();
		qw.eq("a.status", StateEnum.ENABLED.getCode());
		if (StringUtils.isNotBlank(code)) {
			qw.eq("a.code", code);
		}
		if (indentId != null) {
			qw.eq("a.indent_id", indentId);
		}
		if (lineId != null) {
			qw.eq("a.line_id", lineId);
		}
		if (proStatus != null && proStatus.length > 0) {
			qw.in("a.pro_status", proStatus);
		}
		//	根据生产状态排序
		// qw.orderByDesc("a.pro_status","a.create_time");
		List<DispatchVo> list = indentDispatchDao.selectFullList(qw);
		list.sort(Comparator
			//	优先根据生产状态排序
			.comparingInt((DispatchVo vo) -> InvoiceProcessEnum.PRO.getCode().equals(vo.getProStatus()) ? 1 : 0)
			//	根据派单时间排序
			.thenComparing(Comparator.comparing(DispatchVo::getCreateTime).reversed())
			//	根据车号排序
			.thenComparing(DispatchVo::getCarCode)
			.reversed()
		);
		/*list.sort(Comparator
			.comparingInt((DispatchVo vo) -> vo.getProStatus() == InvoiceProcessEnum.PRO.getCode() ? 1 : 0)
			.reversed() // 保持生产状态的降序排序
			.thenComparing(Comparator.comparing(DispatchVo::getCreateTime).reversed()) // 反转派单时间排序
			.thenComparing(DispatchVo::getCarCode) // 车号保持原来的升序排序
		);*/
		return list;
	}


	@Override
	public List<ProductionIndentDispatch> queryByInvoice(Long invoiceId) {
		if (invoiceId == null) {
			return null;
		}
		QueryWrapper<ProductionIndentDispatch> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.eq("invoice_id", invoiceId);
		List<ProductionIndentDispatch> productionIndentDispatchs = indentDispatchDao.selectList(qw);
		return productionIndentDispatchs;
	}

	@Override
	public List<DispatchVo> queryFullByInvoice(String erpDb, Long invoiceId, Integer status) {
		if (invoiceId == null) {
			return null;
		}
		List<DispatchVo> list = indentDispatchDao.queryFullByInvoice(erpDb, invoiceId, status);
		return list;
	}

	@Override
	public List<ProductionIndentDispatch> findByIndentIds(List<Long> indentIds) {
		QueryWrapper<ProductionIndentDispatch> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.in("indent_id", indentIds);
		List<ProductionIndentDispatch> dispatches = indentDispatchDao.selectList(qw);
		return dispatches;
	}

	@Override
	public Double queryInvProStere(Long invoiceId) {
		if (invoiceId == null) {
			return 0d;
		}
		Double stere = indentDispatchDao.queryInvProStere(invoiceId);
		return stere;
	}
}
